// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// l [author] rhyme_lph
// f [email]  rhymelph@gmail.com
// o [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/provider/dashboard/products_services/products_and_services_category_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/widgets/button/add_button.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/input/auto_complete_input_field.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';

@RouterMeta(paramName: "productsAndServicesCategoryPage")
class ProductsAndServicesCategoryPage extends StatefulWidget {
  final String type;
  final List<String> categories;

  const ProductsAndServicesCategoryPage({Key key, this.categories, this.type})
      : super(key: key);

  @override
  _ProductsAndServicesCategoryPageState createState() =>
      _ProductsAndServicesCategoryPageState();
}

class _ProductsAndServicesCategoryPageState
    extends State<ProductsAndServicesCategoryPage>
    with
        PagePresenter<ProductsAndServicesCategoryPage,
            ProductsAndServicesCategoryProvider> {
  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.category,
        actions: [],
      );

  @override
  void initState() {
    provider.initWidgetData(widget.type, widget.categories);
    super.initState();
  }

  @override
  Widget get buildWidget => ListView(
        children: [
          listBuilder<int>(
            selector: (_, __) => __.controllers?.length,
            builder: (BuildContext context, int value, Widget child) => Column(
              children: List.generate(
                  value,
                  (index) => listBuilder<List<String>>(
                        selector: (_, __) =>
                            __.tags.map((e) => e.name).toList(),
                        builder: (BuildContext context, List<String> value,
                                Widget child) =>
                            AutoCompleteInputField(
                          options: provider.tags.map((e) => e.name).toList(),
                          controller: provider.controllers[index],
                          focusNode: provider.focusNodes[index],
                          onRemove: () => provider.onRemove(index),
                        ),
                      )),
            ),
          ),
          listBuilder<bool>(
            selector: (_, __) => __.controllers.length == 0,
            builder: (context, bool value, child) => NormalFormWrapper(
              child: Visibility(
                visible: value,
                child: AddButton(
                  title: S.current.add_category,
                  onTap: provider.onAddCategory,
                ),
              ),
            ),
          ),
          AppSpaces.h30,
          NormalButton(
            onTap: provider.onDone,
            title: S.current.done,
          ),
        ],
      );
}
